import 'package:cached_network_image/cached_network_image.dart';
import 'package:casdon/common/widget/common_loading_widget.dart';
import 'package:flutter/material.dart';

class CircleAvatarWidget extends StatelessWidget {
  final String? imageUrl;
  final double radius;
  final Widget defaultAvatar;
  final BoxFit fit;
  final Color? backgroundColor;

  const CircleAvatarWidget({
    super.key,
    this.imageUrl,
    this.radius = 40,
    this.defaultAvatar = const Icon(Icons.person, size: 40),
    this.fit = BoxFit.cover,
    this.backgroundColor,
  });

  @override
  Widget build(BuildContext context) {
    return CircleAvatar(
      radius: radius,
      backgroundColor:
          backgroundColor ??
          Theme.of(context).colorScheme.primary.withOpacity(0.2),
      child: ClipOval(child: _buildAvatarContent()),
    );
  }

  Widget _buildAvatarContent() {
    if (imageUrl == null || imageUrl!.isEmpty) {
      return SizedBox(
        width: radius * 2,
        height: radius * 2,
        child: Center(child: defaultAvatar),
      );
    }

    return CachedNetworkImage(
      imageUrl: imageUrl!,
      width: radius * 2,
      height: radius * 2,
      fit: fit,
      placeholder: (context, url) => SizedBox(
        width: radius * 2,
        height: radius * 2,
        child: Center(child: const CommonLoadingWidget()),
      ),
      errorWidget: (context, url, error) => SizedBox(
        width: radius * 2,
        height: radius * 2,
        child: Center(child: defaultAvatar),
      ),
    );
  }
}
